package cn.hrlly.tao2shou.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.domain.Application;
import cn.hrlly.tao2shou.other.Config;
import cn.hrlly.tao2shou.presenter.ApplicationDetailsPresenter;
import cn.hrlly.tao2shou.presenter.ApplicationDetailsPresenterImpl;
import cn.hrlly.tao2shou.utils.ImageUtil;
import cn.hrlly.tao2shou.view.base.BaseActivity;
import cn.hrlly.tao2shou.view.view_interface.ApplicationDetailsView;

public class ApplicationDetailsActivity extends BaseActivity implements ApplicationDetailsView {

    @Bind(R.id.ivCommodityPhoto)
    ImageView ivCommodityPhoto;
    @Bind(R.id.tvCommodityName)
    TextView tvCommodityName;
    @Bind(R.id.layoutCommodity)
    LinearLayout layoutCommodity;
    @Bind(R.id.tvSellerNickname)
    TextView tvSellerNickname;
    @Bind(R.id.tvBuyerNickname)
    TextView tvBuyerNickname;
    @Bind(R.id.tvTime)
    TextView tvTime;
    @Bind(R.id.tvLocation)
    TextView tvLocation;
    @Bind(R.id.tvPrice)
    TextView tvPrice;
    @Bind(R.id.tvApplicationTime)
    TextView tvApplicationTime;
    @Bind(R.id.tvBarterName)
    TextView tvBarterName;
    @Bind(R.id.tvBarterDescription)
    TextView tvBarterDescription;
    @Bind(R.id.layoutBarter)
    LinearLayout layoutBarter;
    private ApplicationDetailsPresenter applicationDetailsPresenter;
    private Intent intent;
    private ImageLoader imageLoader;
    private String releaseId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_application_details);
        //绑定控件
        ButterKnife.bind(this);
        //初始化
        intent=getIntent();
        imageLoader=ImageLoader.getInstance();
        applicationDetailsPresenter=new ApplicationDetailsPresenterImpl(this);
        applicationDetailsPresenter.showApplicationDetails();
    }

    //layoutCommodity的点击事件
    @OnClick(R.id.layoutCommodity)
    public void commodity(){
        Intent intent=new Intent(ApplicationDetailsActivity.this,CommodityDetailsActivity.class);
        intent.putExtra(Config.key_release_id,releaseId);
        startActivity(intent);
    }

    //以下是对activity的操作
    @Override
    public String getApplicationId() {
        return intent.getStringExtra(Config.key_application_id);
    }

    @Override
    public void showApplicationDetail(Application application) {
        releaseId=application.getReleaseId();
        if(application.getTradeMethod()==0){
            layoutBarter.setVisibility(View.VISIBLE);
            tvBarterName.setText(application.getBarterName());
            tvBarterDescription.setText(application.getBarterDescription());
            //这里还需要有viewpager的操作
        }
        tvApplicationTime.setText(application.getApplicationTime());
        tvPrice.setText(String.valueOf(application.getExpectedPrice()));
        tvTime.setText(application.getTradeTime());
        tvLocation.setText(application.getTradeLocation());
        tvCommodityName.setText(application.getCommodityName());
        tvSellerNickname.setText(application.getSellerNickname());
        tvBuyerNickname.setText(application.getBuyerNickname());
        imageLoader.displayImage(application.getCommodityPhoto(),ivCommodityPhoto, ImageUtil.getOptions());
    }

    @Override
    public void showMessage(String errorMessage) {
        Toast.makeText(this,errorMessage,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showMessage(int errorMessage) {
        Toast.makeText(this,errorMessage,Toast.LENGTH_SHORT).show();
    }
}
